package com.example.ui;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.base.BaseApp;
import com.example.damain.FileInfo;
import com.example.mepiaplayerv4.R;
import com.example.utils.ViewUtil;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;

/**
 * @author 作者ylh E-mail:504367857@qq.com
 * @version 创建时间：2015-1-20 上午10:55:01 类说明
 */
public class MyActivity extends Activity {

	// 2activity=>myservice 播放音乐
	private static final String ACTION_PLAY = "com.share.ylh.mediaplayer.PLAY";
	// myservice=>2activity 保存状态
	private static final String ACTION_SAVESTATE = "com.share.ylh.mediaplayer.SAVESTATE";
	// myservice->MyActivity 更新进度条
	private static final String ACTION_progressbar = "com.share.ylh.mediaplayer.progressbar";

	private DbUtils db;
	List<FileInfo> list;
	private ImageView play, pause, pre, next, repeat, shuffle, replay;
	private ProgressBar progressBar;
    private IntentFilter intentFilter = null;
    
	private static int id = 1; // 文件id
	private int STATE;// 1:play,2:psuse,3:stop;//7：上一首 8：下一首
	private int playstate = 4;// 4:顺序播放;5：单曲循环播放；6：随机；

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my);

		initView();
		initEvent();
	}

	private void initView() {

		try {
			db = DbUtils.create(this);
			list = db.findAll(FileInfo.class);// 通过类型查找
		} catch (DbException e) {
			e.printStackTrace();
		}

		play = (ImageView) findViewById(R.id.playerplaybtn);
		pause = (ImageView) findViewById(R.id.playerpausebtn);
		pre = (ImageView) findViewById(R.id.playerprebtn);
		next = (ImageView) findViewById(R.id.playernextbtn);
		repeat = (ImageView) findViewById(R.id.repeat);
		shuffle = (ImageView) findViewById(R.id.shuffle);
		replay = (ImageView) findViewById(R.id.replay);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);

	}

	private void initEvent() {

		// 播放
		play.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				STATE = 1;
				playing();
			}
		});

		// 暂停
		pause.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				STATE = 2;
				sendOperateBroadCast();
				pause.setVisibility(View.GONE);
				play.setVisibility(View.VISIBLE);
			}
		});

		// 上一首
		pre.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ((id - 1) >= 0) {
					id = id - 1;
				} else {
					id = 0;
					Toast.makeText(getApplicationContext(), "已经是第一首歌",
							Toast.LENGTH_SHORT).show();
				}
				STATE = 9;
				playing();
			}
		});

		// 下一首
		next.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if ((id + 1) <= list.size()) {
					id = (id + 1);
				} else {
					id = list.size();
					Toast.makeText(getApplicationContext(), "已经是最后一首歌",
							Toast.LENGTH_SHORT).show();
				}
				STATE = 9;
				playing();

			}
		});

		// 顺序->重复
		repeat.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				STATE = 0;
				playstate = 5;
				// ViewUtil.setShardPString("playstate", "5");
				sendOperateBroadCast();
				repeat.setVisibility(View.GONE);
				replay.setVisibility(View.VISIBLE);
			}

		});

		// 重复->随机
		replay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				STATE = 0;
				playstate = 6;
				// ViewUtil.setShardPString("playstate", "6");
				sendOperateBroadCast();
				replay.setVisibility(View.GONE);
				shuffle.setVisibility(View.VISIBLE);
			}
		});

		// 随机->顺序
		shuffle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				STATE = 0;
				playstate = 4;
				// ViewUtil.setShardPString("playstate", "4");
				sendOperateBroadCast();
				shuffle.setVisibility(View.GONE);
				repeat.setVisibility(View.VISIBLE);
			}
		});

		progressBar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setProgress(100);
			}
		});
	}

	private void playing() {

		sendOperateBroadCast();
		play.setVisibility(View.GONE);
		pause.setVisibility(View.VISIBLE);
	}

	/**
	 * 退出进来后保持播放类型，顺序，随机，重复 根据state判断
	 */
	private void stayPlayType() {
		if (STATE == 1) {
			play.setVisibility(View.GONE);
			pause.setVisibility(View.VISIBLE);
		} else if (STATE == 2) {
			pause.setVisibility(View.GONE);
			play.setVisibility(View.VISIBLE);
		}

		if (playstate == 4) {
			// 顺序
			shuffle.setVisibility(View.GONE);
			replay.setVisibility(View.GONE);
			repeat.setVisibility(View.VISIBLE);
		} else if (playstate == 5) {
			// 重复
			repeat.setVisibility(View.GONE);
			shuffle.setVisibility(View.GONE);
			replay.setVisibility(View.VISIBLE);
		} else if (playstate == 6) {
			// 6
			replay.setVisibility(View.GONE);
			repeat.setVisibility(View.GONE);
			shuffle.setVisibility(View.VISIBLE);
		} else {
			// 默认顺序播放
			shuffle.setVisibility(View.GONE);
			replay.setVisibility(View.GONE);
			repeat.setVisibility(View.VISIBLE);
		}
	}

	private void sendOperateBroadCast() {
		Log.i("MYACT sendOperateBroadCast", STATE + "===" + id);
		stayPlayType();
		Intent intent = new Intent(ACTION_PLAY);
		intent.putExtra("id", id);// 文件id（1,2,3） 不是路径
		intent.putExtra("STATE", STATE);// 文件id（1,2,3） 不是路径
		intent.putExtra("playstate", playstate);// 需要运算的用int，另外用字符串
		BaseApp.AppContext.sendBroadcast(intent);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 解除注册
		unregisterReceiver(receiver);
	}

	@Override
	protected void onResume() {
		super.onResume();

		// 接收广播
		registerReceiver(receiver, init());
	}

	private IntentFilter init() {
		if (intentFilter == null) {
			intentFilter = new IntentFilter();
			intentFilter.addAction(ACTION_progressbar);
			intentFilter.addAction(ACTION_SAVESTATE);
		}
		return intentFilter;
	}

	// 接收跟新进度条的广播
	private BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(ACTION_progressbar)) {
				int Current = intent.getIntExtra("Current", 0);
				int Duration = intent.getIntExtra("Duration", 0);
				progressBar.setMax(Duration);
				progressBar.setProgress(Current);
			}

			if (intent.getAction().equals(ACTION_SAVESTATE)) {
				id = intent.getIntExtra("id", 0);
				STATE = intent.getIntExtra("STATE", 1);
				playstate = intent.getIntExtra("playstate", 4);
				stayPlayType();
			}
		}
	};
}
